package org.gdczkj.practical;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App {

	/**
	 * 查找.
	 * 
	 * @return Student.
	 */
	public static Student findStudent(List<Student> list) {
		final Student stu;
		if (null != list && !list.isEmpty()) {
			stu = list.get(0);
		} else {
			stu = new Student();
		}
		return stu;
	}

	/**
	 * 修改名称.
	 * 
	 * @param stu
	 *            实例.
	 */
	public static void modifyStudentName(Student stu, String name) {
		stu.setName(name);
	}

	public static void main(String[] args) throws CloneNotSupportedException {
		final Student stu = new Student();
		stu.setName("kitty");
		stu.setAge(18);

		System.out.println("Your name is " + stu.getName());

		final Student stu2 = (Student) stu.clone();
		modifyStudentName(stu2, "polaris");
		System.out.println("Your name is " + stu.getName());
		
		
		
		BigDecimal val1 = new BigDecimal(Double.toString(0.3333));
		BigDecimal val2 = new BigDecimal(Double.toString(0.555555));
		
		System.out.println("add'value:" + val1.add(val2));
		System.out.println("substrat's value:" + val1.subtract(val2));
		System.out.println("multi‘s value" + val1.multiply(val2));
		System.out.println("divide's value" + val1.divide(val2,3, BigDecimal.ROUND_HALF_UP));
	}

	public void copyArray1(int[] src, int[] desc) {
		final int size = src.length;
		for (int i = 0; i < size; i++) {
			desc[i] = src[i];
		}
	}

	public void copyArray2(int[] src, int[] desc) throws Exception {
		final int size = src.length;
		System.arraycopy(src, 0, desc, 0, size);
		try {

		} catch (Exception e) {
			// TODO: handle exception
			throw new Exception(e);
		}
	}

	public void readFile(final String uri) throws IOException {
		int data = 0;
		InputStream inputStream = null;
		try {
		    inputStream = new FileInputStream(new File(uri));
			data = inputStream.read();
			while (data != 0) {
				// todo...
			}
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			if(null!=inputStream) {
				inputStream.close();
			}
		}

		// handle resouce close;
	}
	
	public void readFile2(final String uri) throws IOException {
		int data = 0;
		InputStream inputStream = null;
		try {
		    inputStream = new FileInputStream(new File(uri));
			while (true) {
				try {
					data = inputStream.read();
					// TODO...
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			if(null!=inputStream) {
				inputStream.close();
			}
		}

		// handle resouce close;
	}	
}
